/**
 * Copyright 2021 Inc.
 **/
package com.myz.rabbitmq.common.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author maoyz0621 on 2021/6/7
 * @version v1.0
 */
@Configuration
// @AutoConfigureAfter(RabbitConfig.class)
public class DirectExchangeConfig {

    /**
     * 1.定义direct exchange，绑定first_exchange
     * 2.durable="true" 持久化交换机， rabbitmq重启的时候不需要创建新的交换机
     * 3.direct交换器相对来说比较简单，匹配规则为：如果路由键匹配，消息就被投送到相关的队列
     * fanout交换器中没有路由键的概念，他会把消息发送到所有绑定在此交换器上面的队列中。
     * topic交换器你采用模糊匹配路由键的原则进行转发消息到队列中
     */
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(RabbitMqConstant.ExchangeEnum.CONTRACT_DIRECT.getCode(), true, false);
    }

    /**
     * 将消息队列1和交换机direct进行绑定,指定队列key
     */
    @Bean
    public Binding bindingDirect(Queue firstQueue) {
        return BindingBuilder.bind(firstQueue)
                .to(directExchange())
                .with(RabbitMqConstant.RoutingKeyEnum.DIRECT_QUEUE_KEY.getCode());
    }
}